package cn.rzpt.demo;


import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.log4j.Logger;
import org.junit.Test;

import cn.rzpt.demo.entity.Address;
import cn.rzpt.demo.entity.User;
import cn.rzpt.demo.mapper.UserMapper;

/**
 * UserMapper 测试
 * @author zsm
 *
 */
public class UserTest {

	Logger logger = Logger.getLogger(getClass());
	
	/**
	 * 测试Save方法
	 */
	//@Test
	public void testSave() {
		SqlSession sqlSession = null;
		try {
			sqlSession = MyBatisUtil.createSession();
			User user = new User();
			user.setId("dc");
			user.setName("邓超");
			user.setPassword("123456");
			user.setPhone("1234567890");
			user.setRoleId(1);
			//执行sql后返回影响数据库行数
			int count = sqlSession.getMapper(UserMapper.class).save(user);
			
			logger.debug("影响行数：" + count);
			//必抛出异常
			int a = 1/0;
			//执行提交操作
			sqlSession.commit();
			
		} catch (Exception e) {
			// TODO: 出现异常执行回滚
			logger.debug("----------------------出现异常执行回滚！");
			sqlSession.rollback();
		}finally {
			//无论成功与否 都执行closeSqlSerssion
			MyBatisUtil.closeSqlSession(sqlSession);
		}
				
	}
	/**
	 * 测试updatePassword
	 */
	//@Test
	public void testUpdatePassword() {
		SqlSession sqlSession = null;
		try {
			sqlSession = MyBatisUtil.createSession();			
			//执行sql后返回影响数据库行数
			int count = sqlSession.getMapper(UserMapper.class).updatePassword("abc", "111111");
			
			logger.debug("影响行数：" + count);
			
			//执行提交操作
			sqlSession.commit();			
		} catch (Exception e) {
			// TODO: 出现异常执行回滚
			logger.debug("----------------------出现异常执行回滚！");
			sqlSession.rollback();
		}finally {
			//无论成功与否 都执行closeSqlSerssion
			MyBatisUtil.closeSqlSession(sqlSession);
		}
	}
	/**
	 * 测试deleteById方法
	 */
	//@Test
	public void testDeleteById() {
		SqlSession sqlSession = null;
		try {
			sqlSession = MyBatisUtil.createSession();			
			//执行sql后返回影响数据库行数
			int count = sqlSession.getMapper(UserMapper.class).deleteById("lisi");
			
			logger.debug("影响行数：" + count);
			
			//执行提交操作
			sqlSession.commit();			
		} catch (Exception e) {
			// TODO: 出现异常执行回滚
			logger.debug("----------------------出现异常执行回滚！");
			sqlSession.rollback();
		}finally {
			//无论成功与否 都执行closeSqlSerssion
			MyBatisUtil.closeSqlSession(sqlSession);
		}
	}
	
	//@Test
	public void testCount() throws IOException {
		//mybatis 配置文件所在位置
//		String resource = "cn/rzpt/demo/mybatis-config.xml";
//		InputStream inputStream = Resources.getResourceAsStream(resource);
//		//通过sqlsessionFactoryBuilder 创建SqlSessionFactory
//		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		
		int count = 0;
		SqlSession session = MyBatisUtil.createSession();
		//count = session.selectOne("cn.rzpt.demo.mapper.UserMapper.count");
		count = session.getMapper(UserMapper.class).count();
		
		logger.debug("tb_user共有记录个数：" + count);
		MyBatisUtil.closeSqlSession(session);
	}
	
	/**
	 * 测试 userMapper 的queryAll方法
	 */
	//@Test
	public void testQueryAll() {
		SqlSession sqlSession = MyBatisUtil.createSession();
		List<User> list = sqlSession.getMapper(UserMapper.class).queryAll();
		logger.debug("list.size: " + list.size());
		//遍历list
		for (int i = 0; i < list.size(); i++) {
			User user = list.get(i);
			
			logger.debug("user.id : " + user.getId() + "; user.name : " + user.getName());
		}
		MyBatisUtil.closeSqlSession(sqlSession);
	}
	
	/**
	 * 测试UserMapper的queryByName方法
	 */
	//@Test
	public void testQueryByName() {
		SqlSession sqlSession = MyBatisUtil.createSession();
		List<User> list = sqlSession.getMapper(UserMapper.class).queryByName("张");
		for (int i = 0; i < list.size(); i++) {
			User user = list.get(i);
			logger.debug(user.toString());
		}
		
		MyBatisUtil.closeSqlSession(sqlSession);		
	}
	/**
	 * 测试UserMapper的queryByNameAndId方法
	 */
	//@Test
	public void testQueryByNameAndId() {
		SqlSession sqlSession = MyBatisUtil.createSession();
		List<User> list = sqlSession.getMapper(UserMapper.class).queryByNameAndId("abc", "张");
		for (int i = 0; i < list.size(); i++) {
			User user = list.get(i);
			logger.debug(user.toString());
		}
		
		MyBatisUtil.closeSqlSession(sqlSession);		
	}
	/**
	 * 测试queryList方法
	 */
	//@Test
	public void testQueryList() {
		SqlSession sqlSession = MyBatisUtil.createSession();
		User param = new User();
		param.setId("abc");
		param.setName("张");
		List<User> list = sqlSession.getMapper(UserMapper.class).queryList(param);
		for (int i = 0; i < list.size(); i++) {
			User user = list.get(i);
			logger.debug(user.toString());
		}
		
		MyBatisUtil.closeSqlSession(sqlSession);	
		
	}
	/**
	 * 测试queryListByMap
	 */
	//@Test
	public void testQueryListByMap() {
		SqlSession sqlSession = MyBatisUtil.createSession();
		Map<String, String> map = new HashMap<String, String>();
		map.put("id", "abc");
		map.put("name", "张");
		List<User> list = sqlSession.getMapper(UserMapper.class).queryListByMap(map);
		for (int i = 0; i < list.size(); i++) {
			User user = list.get(i);
			logger.debug(user.toString());
		}
		
		MyBatisUtil.closeSqlSession(sqlSession);	
		
	}
	
	/**
	 * 测试QueryUserAddressByUserId方法
	 */
	//@Test
	public void testQueryUserAddressByUserId() {
		SqlSession sqlSession = MyBatisUtil.createSession();
		List<User> list = sqlSession.getMapper(UserMapper.class).queryUserAddressByUserId("dc");
		for (User user: list) {
			logger.debug("=======>user:" +  user.toString());
			for(Address address :  user.getAddresses()) {
				logger.debug("=========>address:" + address.toString());
			}
		}
		
		MyBatisUtil.closeSqlSession(sqlSession);	
		
	}
	
	/**
	 * 测试QueryUserByNameAndRoleId方法
	 */
	//@Test
	public void testQueryUserByNameAndRoleId() {
		SqlSession sqlSerssion  =MyBatisUtil.createSession();
		
		List<User> list = sqlSerssion.getMapper(UserMapper.class).queryUserByNameAndRoleId("", null);
		for(User user: list) {
			logger.debug("=======>user:" +  user.toString());			
		}
		
		MyBatisUtil.closeSqlSession(sqlSerssion);	
	}
	
	//@Test
	public void testUpdate() {
		SqlSession sqlSession = null;
		try {
			sqlSession = MyBatisUtil.createSession();		
			User user = new User();
			user.setId("test");
			user.setName("测试员");
			user.setPassword("123");
			user.setPhone("");
			user.setRoleId(1);
			
			//执行sql后返回影响数据库行数
			int count = sqlSession.getMapper(UserMapper.class).update(user);
			
			logger.debug("影响行数：" + count);
			
			//执行提交操作
			sqlSession.commit();			
		} catch (Exception e) {
			// TODO: 出现异常执行回滚
			logger.debug("----------------------出现异常执行回滚！");
			sqlSession.rollback();
		}finally {
			//无论成功与否 都执行closeSqlSerssion
			MyBatisUtil.closeSqlSession(sqlSession);
		}
	}
	
	/**
	 * 测试QueryUserByRoleIds方法
	 */
	//@Test
	public void testQueryUserByRoleIds() {
		SqlSession sqlSession = MyBatisUtil.createSession();
//		传入数组
//		Integer[] roleIds = {1,2};
//		List<User> users = sqlSession.getMapper(UserMapper.class).queryUserByRoleIds(roleIds);
		
		//传入List 作为参数
//		List<Integer> roleIds = new ArrayList<>();
//		roleIds.add(1);
//		roleIds.add(2);		
//		List<User> users = sqlSession.getMapper(UserMapper.class).queryUserByRoleIdList(roleIds);
		

		List<Integer> roleIds = new ArrayList<>();
		roleIds.add(1);
		roleIds.add(2);	
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("name", null);
		params.put("roleIds", roleIds);

		List<User> users = sqlSession.getMapper(UserMapper.class).queryUserByNameRoleIds(params);		
		for(User user: users) {
			logger.debug("user===========>" + user.toString());
		}		
		
		MyBatisUtil.closeSqlSession(sqlSession);
	}
	
	/**
	 * 测试QueryUserByChoose 方法
	 */
	//@Test
	public void testQueryUserByChoose() {
		SqlSession sqlSession = MyBatisUtil.createSession();
		User userTemp  =new User();
//		userTemp.setName("张");
//		userTemp.setRoleId(3);
		
		List<User> users = sqlSession.getMapper(UserMapper.class).queryUserByChoose(userTemp);	
		//遍历打印结果
		for(User user: users) {
			logger.debug("user===========>" + user.toString());
		}
		
		MyBatisUtil.closeSqlSession(sqlSession);
	}
	/**
	 * 测试QueryUserPage方法
	 */
	@Test
	public void testQueryUserPage() {
		SqlSession sqlSession = MyBatisUtil.createSession();
		int pageNo = 2;
		int pageSize =3;
		List<User> users = sqlSession.getMapper(UserMapper.class).queryUserPage(pageSize, (pageNo-1) * pageSize, "", "");
		//遍历打印结果
		for(User user: users) {
			logger.debug("user===========>" + user.toString());
		}
		
		MyBatisUtil.closeSqlSession(sqlSession);
	}
}
